package cn.aylog.test;

import cn.aylog.conf.BeansConfig;
import cn.aylog.pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Demo {

//    @Test
//    public void run1(){
//        String path = "classpath:application.xml";
//        ApplicationContext context = new ClassPathXmlApplicationContext(path);
//        User user = context.getBean(User.class);
//        user.setName("aylog");
//        user.setAge(18);
//        System.out.println(user);
//    }

    @Test
    public void run2() {
        String path = "classpath:application.xml";
        ApplicationContext context = new ClassPathXmlApplicationContext(path);
        User bean = context.getBean(User.class);
        System.out.println(bean);
        User bean2 = context.getBean(User.class);
        System.out.println(bean2);
        AbstractApplicationContext ac = (AbstractApplicationContext) context;
        ac.registerShutdownHook();
        System.gc();
    }
    @Test
    public void run3() {
        // 使用java配置类的方式进行配置属性
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
        // 添加/注册配置类
        ctx.register(BeansConfig.class);
        // 属性配置, 必须强制刷新!!!
        ctx.refresh();
        User bean = ctx.getBean(User.class);
        System.out.println(bean);
    }

    @Test
    public void run4() {
        ApplicationContext ctx = new AnnotationConfigApplicationContext(BeansConfig.class);
        User bean = ctx.getBean(User.class);
        System.out.println(bean);
    }



}
